package com.hmkj.core.mapper.simulation;

import com.hmkj.common.baseMapper.GenericMapper;
import com.hmkj.core.model.index.OrderSimulationModel;
import com.hmkj.core.po.simulation.OrderSimulation;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * 模拟订单相关Dao接口<br>
 *
 * @author eproo
 * @date   2018-07-09 02:35:10
 */
public interface OrderSimulationMapper extends GenericMapper<OrderSimulation, Integer> {

    /**
     * 根据订单号查询
     *
     * @param orderNo
     * @return
     */
    @Select("SELECT t.* FROM biz_order_simulation t WHERE 1 = 1 and t.order_no = #{orderNo}")
    OrderSimulation selectByOrderNo(@Param(value = "orderNo")String orderNo);

    @Select("SELECT SUM(t.profit_count) FROM biz_order_simulation t WHERE 1=1 AND t.del_flag = 1 AND t.user_id = #{userId} AND t.status = #{status} ")
    BigDecimal sumProfit(@Param(value = "userId") Integer userId, @Param(value = "status") Integer status);

    /**
     * 模拟点卖列表
     *
     * @param param 参数集合
     * @return List<OrderSimulation>
     */
    @Select(" <script> SELECT t.* " +
            "FROM biz_order_simulation t " +
            "WHERE t.user_id = #{userId} AND " +
            "t.status = #{status} AND " +
            "t.del_flag = 1 " +
            "ORDER BY t.status desc, t.id desc </script>")
    List<OrderSimulationModel> findListForOrderSimulation(Map<String, Object> param);

    /**
     * 模拟点卖列表
     *
     * @param param 参数集合
     * @return Integer
     */
    @Select(" <script> SELECT count(*) " +
            "FROM biz_order_simulation t " +
            "WHERE t.user_id = #{userId} " +
            "AND t.status = #{status} " +
            "AND t.del_flag = 1 " +
            "ORDER BY t.status desc, t.id desc </script>")
    Integer findCountForOrderSimulation(Map<String, Object> param);

    /**
     * 模拟结算列表
     *
     * @param param 参数集合
     * @return List<OrderSimulation>
     */
    @Select(" <script> SELECT t.* " +
            "FROM biz_order_simulation t " +
            "WHERE t.user_id = #{userId} " +
            "<if test=\"date != null and date == 1\"> AND <![CDATA[ DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(t.create_time) ]]></if>" +
            "<if test=\"date != null and date == 2\"> AND <![CDATA[ DATE_SUB(CURDATE(), INTERVAL INTERVAL 1 MONTH) <= date(t.create_time) ]]></if>" +
            "<if test=\"date != null and date == 3\"> AND <![CDATA[ DATE_SUB(CURDATE(), INTERVAL INTERVAL 3 MONTH) <= date(t.create_time) ]]></if>" +
            "<if test=\"date != null and date == 4\"> AND <![CDATA[ DATE_SUB(CURDATE(), INTERVAL INTERVAL 1 YEAR) <= date(t.create_time) ]]></if>" +
            "AND t.status IN (#{status})  " +
            "AND t.del_flag = 1 " +
            "ORDER BY t.status asc ,t.settle_time desc </script>")
    List<OrderSimulationModel> findBalanceForOrderSimulation(Map<String, Object> param);

    /**
     * 模拟点卖列表
     *
     * @param param 参数集合
     * @return Integer
     */
    @Select(" <script> SELECT count(*) " +
            "FROM biz_order_simulation t " +
            "WHERE t.user_id = #{userId} " +
            "<if test=\"date != null and date == 1\"> AND <![CDATA[ DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(t.create_time) ]]></if>" +
            "<if test=\"date != null and date == 2\"> AND <![CDATA[ DATE_SUB(CURDATE(), INTERVAL INTERVAL 1 MONTH) <= date(t.create_time) ]]></if>" +
            "<if test=\"date != null and date == 3\"> AND <![CDATA[ DATE_SUB(CURDATE(), INTERVAL INTERVAL 3 MONTH) <= date(t.create_time) ]]></if>" +
            "<if test=\"date != null and date == 4\"> AND <![CDATA[ DATE_SUB(CURDATE(), INTERVAL INTERVAL 1 YEAR) <= date(t.create_time) ]]></if>" +
            "AND t.status IN (#{status}) " +
            "AND t.del_flag = 1 " +
            "ORDER BY t.status asc ,t.settle_time desc </script> ")
    Integer findBalanceCountForOrderSimulation(Map<String, Object> param);

    /**
     * 询某一时间段的模拟结算订单
     *
     * @param param
     * @return
     */
    @Select({"<script>SELECT t.* FROM biz_order_simulation t WHERE t.user_id = #{userId} " +
            "<if test=\"date != null and date == 1\"> AND <![CDATA[ to_days(t.create_time) = to_days(now()) ]]></if>" +
            "<if test=\"date != null and date == 7\"> AND <![CDATA[ DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(t.create_time) ]]></if>" +
            "<if test=\"date != null and date == 30\"> AND <![CDATA[ DATE_SUB(CURDATE(), INTERVAL 1 MONTH) <= date(t.create_time) ]]></if>" +
            "<if test=\"date != null and date == 90\"> AND <![CDATA[ DATE_SUB(CURDATE(), INTERVAL 3 MONTH) <= date(t.create_time) ]]></if>" +
            "<if test=\"date != null and date == 365\"> AND <![CDATA[ DATE_SUB(CURDATE(),INTERVAL 1 YEAR) <= date(t.create_time) ]]></if>" +
            "<if test=\"status != null and status != ''\"> AND t.status = #{status} </if>" +
            "<if test=\"statuStr != null and statuStr != ''\">" +
            " AND t.status IN  <foreach collection=\"statuStr\" index=\"index\" item=\"item\" open=\"(\" separator=\",\" close=\")\"> #{item} </foreach>" +
            "</if>" +
            "AND t.del_flag = 1 " +
            "ORDER BY t.status asc ,t.settle_time desc </script>"})
    List<OrderSimulation> findOrderSimulateForTime(Map<String, Object> param);

    /**
     * 询某一时间段的模拟结算订单
     *date(t.create_time)
     * @param param
     * @return
     */
    @Select("<script>SELECT t.* FROM biz_order_simulation t WHERE t.user_id = #{userId} " +
            "<if test=\"date != null and date == 1\"> AND <![CDATA[ to_days(t.create_time) = to_days(now()) ]]></if>" +
            "<if test=\"date != null and date == 7\"> AND <![CDATA[ DATE_SUB(CURDATE(), INTERVAL 7 DAY) <= date(t.create_time) ]]></if>" +
            "<if test=\"date != null and date == 30\"> AND <![CDATA[ DATE_SUB(CURDATE(), INTERVAL 1 MONTH) <= date(t.create_time) ]]></if>" +
            "<if test=\"date != null and date == 90\"> AND <![CDATA[ DATE_SUB(CURDATE(), INTERVAL 3 MONTH) <= date(t.create_time) ]]></if>" +
            "<if test=\"date != null and date == 365\"> AND <![CDATA[ DATE_SUB(CURDATE(), INTERVAL 1 YEAR) <= date(t.create_time) ]]></if>" +
            "<if test=\"status != null and status != ''\"> AND t.status = #{status} </if>" +
            "AND t.profit_count >= 0 " +
            "AND t.del_flag = 1 " +
            "ORDER BY t.status asc ,t.settle_time desc </script>")
    List<OrderSimulation> findProfitOrderSimulateForTime(Map<String, Object> param);

    @Select("select user_id,SUM(profit_count) as profit_count from biz_order_simulation where sell_deal_time > #{today}  group by user_id  order by profit_count desc")
    List<OrderSimulation> selectSimulationRank(@Param(value = "today")String today);

}
